﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

using Gift.WebApp;
using Gift.Model;


public partial class admin_manager_manager : BaseAdmin
    {

    tbAdminLogin oAdminLogin;


    string AdminId
    {
        get
        {
            return base.Request.QueryString["id"];
        }
    }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (AdminId == null)
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.ManagerAdd);
                }
                else
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.ManagerEdit);
                }
                this.BindDepartment();
                this.BildForm();
            }

        }

        private void BildForm()
        {
            if (AdminId != null)
            {
                this.txtUser.ReadOnly = true;
                oAdminLogin = BaseFactory.theAdmin.GetModel(int.Parse(AdminId));
                this.txtUser.Text = oAdminLogin.AdminUser;
                this.txtName.Text = oAdminLogin.AdminName;
                this.txtPwd.Text = oAdminLogin.AdminPwd;
                this.txtPwd2.Text = oAdminLogin.AdminPwd;
                this.txtDesc.Text = oAdminLogin.AdminDesc;
                if (this.ddlDepartment.Items.FindByValue(oAdminLogin.DepartmentID.ToString()) != null)
                {
                    this.ddlDepartment.Items.FindByValue(oAdminLogin.DepartmentID.ToString()).Selected = true;
                }
            }
        }

        void BindDepartment()
        {
            DataSet ds = BaseFactory.theDepartment.GetList(string.Empty);
            this.ddlDepartment.DataTextField = "DepName";
            this.ddlDepartment.DataValueField = "Id";
            this.ddlDepartment.DataSource = ds;
            this.ddlDepartment.DataBind();
            this.ddlDepartment.Items.Insert(0, new ListItem("请选择部门...", ""));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
           
             if (base.Request.QueryString["id"] != null)
            {
             
                DataTable table = SQLAccess.GreatDs("SELECT AdminPwd FROM tbAdminlogin where Adminid=" + AdminId + "").Tables[0];

                oAdminLogin = BaseFactory.theAdmin.GetModel(int.Parse(AdminId));
                oAdminLogin.AdminUser = re.replace(this.txtUser.Text);
                oAdminLogin.AdminName = re.replace(this.txtName.Text);
                oAdminLogin.AdminFlag = 1;
                if (this.txtPwd.Text.Trim() != "")
                {
                    oAdminLogin.AdminPwd = re.EncodeMD5(this.txtPwd.Text.Trim());
                }
                oAdminLogin.AdminDesc = re.replace(this.txtDesc.Text);
                oAdminLogin.DepartmentID = int.Parse(this.ddlDepartment.SelectedItem.Value);
                BaseFactory.theAdmin.Update(oAdminLogin);

                base.Response.Write("<script language='javascript'>;alert('修改成功!');window.location.href='ManagerList.aspx';</script>");
            }
            else
            {
                if (re.replace(this.txtPwd.Text) == "")
                {
                    base.Response.Write("<script language='javascript'>;alert('密码不能为空!');</script>");
                }
                else if (re.replace(this.txtPwd.Text) != re.replace(this.txtPwd2.Text))
                {
                    base.Response.Write("<script language=javascript>;alert('密码不相同!');</script>");
                }
                oAdminLogin = BaseFactory.theAdmin.GetModelByAdminUser(re.replace(this.txtUser.Text));
                if (oAdminLogin != null)
                {
                    base.Response.Write("<script>alert('对不起，此登录用户名已存在，请用别的用户名！');</script>");
                }
                else
                {
                    oAdminLogin = new tbAdminLogin();
                    oAdminLogin.AdminUser = re.replace(this.txtUser.Text);
                    oAdminLogin.AdminName = re.replace(this.txtName.Text);
                    oAdminLogin.AdminFlag = 1;
                    oAdminLogin.AdminPwd = re.EncodeMD5(this.txtPwd.Text.Trim());
                    oAdminLogin.AdminDesc = re.replace(this.txtDesc.Text);
                    oAdminLogin.DepartmentID = int.Parse(this.ddlDepartment.SelectedItem.Value);
                    BaseFactory.theAdmin.Add(oAdminLogin);
                    base.Response.Write("<script language='javascript'>;alert('添加成功');window.location.href='ManagerList.aspx';</script>");
                }
            }
        }


    }
